//
//  MYVisitorView.swift
//  MYWeiBo
//
//  Created by apple on 16/5/12.
//  Copyright © 2016年 YanMo. All rights reserved.
//

import UIKit
//定义代理
@objc protocol MYVisitorViewDelegate:NSObjectProtocol{

    //代理方法
    func vistorViewDidClickRegister()
    func vistorViewDidClickLogin()
    

}

class MYVisitorView: UIView {

    //定义代理属性
    weak var delegate:MYVisitorViewDelegate?
    
    //公开一个方法，用于设置不同界面
    func setupVistorInfo(imageName:String,message:String){
        iconView.image = UIImage(named:imageName)
        messageLabel.text = message
        iconView.hidden = true
        coverView.hidden = true

    }
    //给首页加上旋转
    func startRotationAnimation(){
        let anim = CABasicAnimation(keyPath:"transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey:nil)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        //准备登录的ui
        prepareUI()
        
    }
    
    //设置ui
    private func prepareUI(){
        
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
    
        // 将子控件添加进来
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(homeView)
        self.addSubview(messageLabel)
        self.addSubview(registerButton)
        self.addSubview(loginButton)
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 添加约束(最好只添加一次)
        // 创建的约束都往要约束的view的父控件上面添加
        
        // 转轮
        /// CenterX和父控件的CenterX重合
        // item: 要添加约束的view
        // attribute: 要添加的View的属性
        // toItem: 要参照的view
        // attribute: 要参照View的属性
        // 将约束添加到view上面去
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        /// CenterY和父控件的CenterY重合
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        // 遮盖
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        // 小房子
        // CenterX转轮CenterX
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // CenterY转轮CenterY
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 消息label
        /// 宽度
        /// 当参照的view为nil时,属性为NSLayoutAttribute.NotAnAttribute
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        // CenterX 和父控件CenterX
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // Top和转轮底部
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 注册按钮
        /// 顶部和label底部
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        /// 左边和label对齐
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        /// 宽度
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        // 高度
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登录按钮
        /// 顶部和label底部
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        /// 右边和label对齐
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        /// 宽度
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        // 高度
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
    }
    
 /// 添加按钮的点击事件
    @objc func didClickRegister(){
       
        delegate?.vistorViewDidClickRegister()
    }
    @objc func didClickLogin(){
    
        delegate?.vistorViewDidClickLogin()
    }
    
    /**
    *  懒加载
    */
    private lazy var iconView:UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var coverView:UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var homeView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.text = "关注一些人，看看有什么惊喜！"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(16)
        label.textAlignment =  NSTextAlignment.Center
        label.numberOfLines = 0
        return label
    
    }()
    //注册按钮
    private lazy var registerButton:UIButton = {
        let  button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        //添加按钮的点击事件
        button.addTarget(self, action:"didClickRegister", forControlEvents:UIControlEvents.TouchUpInside)
        return button
 
    }()
    //登录按钮
    private lazy var loginButton:UIButton = {
        let  button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        //添加按钮的点击事件
        button.addTarget(self, action:"didClickLogin", forControlEvents:UIControlEvents.TouchUpInside)
        return button

    }()
    
 
}
















